package com.mybatisplus.gencode;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.IFill;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collections;

public class CodeGenerator {


//    private static final String Url = "jdbc:mysql://124.71.99.94:3306/vshop_saas?" +
//            "useUnicode=true&characterEncoding=utf-8&useSSL=false&useInformationSchema=true&remarks=true&serverTimezone=Asia/Shanghai";
//    private static final String UserName = "vshop_saas";
//    private static final String Password = "vshop_saas123456";

    private static final String Url = "jdbc:mysql://124.71.99.94:3306/vipboot?" +
            "useUnicode=true&characterEncoding=utf-8&useSSL=false&useInformationSchema=true&remarks=true&serverTimezone=Asia/Shanghai";
    private static final String UserName = "vipboot";
    private static final String Password = "vipboot123456";

    public static void main(String[] args) {
        FastAutoGenerator.create(Url, UserName, Password)
                .globalConfig(builder -> {
                    builder.author("daze")
                            // 指定输出目录
                            .outputDir(System.getProperty("user.dir") + "/src/main/java")
                            .dateType(DateType.ONLY_DATE) // 设置时间类型策略
                            .disableServiceInterface()
                            .disableOpenDir();
                })
                .packageConfig(builder -> {
                    builder.parent("com.vipboot.platform") // 设置父包名
                            .moduleName("system") // 设置父包模块名
                            .entity("domain.entity")
                            .mapper("crud.mapper")
//                            .service("crud.service")
                            .serviceImpl("crud.dao")
                            // 设置mapperXml生成路径
                            .pathInfo(Collections.singletonMap(OutputFile.xml,
                                    System.getProperty("user.dir") + "/src/main/resources/mapper/system"));
                })
                .strategyConfig(builder -> {
                    // 设置需要生成的表名
//                    builder.addInclude("t_simple")
                    // 设置过滤表前缀
                    builder
                            .addTablePrefix("")
                            .entityBuilder()
                            .javaTemplate("/templates/entity.java")
                            .enableLombok() // 开启 Lombok
                            .enableFileOverride()
                            .logicDeleteColumnName("delete_flag")
                            .addTableFills(
                                    new Column("create_dept", FieldFill.INSERT),
                                    new Column("create_time", FieldFill.INSERT),
                                    new Column("create_by", FieldFill.INSERT),
                                    new Column("update_time", FieldFill.UPDATE),
                                    new Column("update_by", FieldFill.UPDATE))
                            .naming(NamingStrategy.underline_to_camel) // 表名生成策略
                            .columnNaming(NamingStrategy.underline_to_camel) // 列名生成策略
                            .controllerBuilder()
                            .enableRestStyle() // 开启 REST 风格控制器
                            .serviceBuilder()
                            .disableService()
                            .superServiceImplClass("com.vipboot.framework.mybatisplus.core.base.dao.PlusDaoImpl")
//                            .formatServiceFileName("%sService") // 格式化 service 文件名
                            .formatServiceImplFileName("%sDao") // 格式化 service 实现类文件名
                            .mapperBuilder()
                            .mapperAnnotation(Mapper.class)
                            .superClass("com.vipboot.framework.mybatisplus.core.base.mapper.PlusBaseMapper");
//                            .enableBaseColumnList() // 开启 baseColumnList
//                            .enableBaseResultMap(); // 开启 baseResultMap
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();
    }

}
